import urllib.request
import urllib.parse

url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFeom=http://www.youdao.com/'

#head可以填写为浏览器的正常访问
#这是第一种方法
#head={}
#head['User-Agent']='Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'

data = {
        "type" : "AUTO",
        "i" : "sentence",
        "doctype" : "json",
        "xmlVersion" : "1.8",
        "keyfrom" : "fanyi.web",
        "ue" : "UTF-8",
        "action" : "FY_BY_CLICKBUTTON",
        "typoResult" : "true"
    }
data=urllib.parse.urlencode(data).encode('utf-8')
#req=urllib.request.Request(url,data,head)
#第二种方法
req=urllib.request.Request(url,data)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')

response=urllib.request.urlopen(req)
html=response.read().decode('utf-8')

print(html)
